Track $arr[$key] existence across array_search/array_find_key via conditional expression holders#5552
Open
phpstan-bot wants to merge 9 commits intophpstan:2.1.xfrom
Open
Conversation
staabm
reviewed
Apr 27, 2026
Contributor
|
can we do similar stuff for |
a413d3c to
af34f1f
Compare
Contributor
|
rebase the pull request and resolve conflicts |
02ca2ee to
67c9b37
Compare
staabm
reviewed
Apr 29, 2026
| (new SpecifiedTypes([], []))->setNewConditionalExpressionHolders([ | ||
| $dimFetchString => [$holder->getKey() => $holder], | ||
| ]), | ||
| $this->create($dimFetch, $arrayType->getIterableValueType(), TypeSpecifierContext::createTrue(), $scope), |
Contributor
There was a problem hiding this comment.
For array_search we could improve the type no ?
If
$key = array_search($needle, $arr)
then
$arr[$key] = $needle
…ia conditional expression holders - Add conditional expression holders in TypeSpecifier for `$key = array_search($needle, $arr)` that fire when `$key !== false`, registering `$arr[$key]` as existing - Add conditional expression holders for `$key = array_find_key($arr, $cb)` that fire when `$key !== null`, registering `$arr[$key]` as existing - Add `array_find_key` to the existing `array_key_first/last !== null` comparison handler to narrow array to non-empty - Move `array_search` true-context handling from standalone block into unified handler alongside the conditional holder logic - Update existing test that was asserting the buggy behavior (separate assignment `$key = array_search(...)` followed by `if ($key !== false)` was reporting "Offset might not exist")
… tracking Extract createArrayDimFetchConditionalExpressionHolder() helper method and merge the separate array_search/array_find_key blocks into one, reducing three instances of the same ~12-line pattern to single-line calls. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…st instead of duplicating Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…le type for strict array_search Both array_search and array_find_key now narrow the array to non-empty when the result is not the sentinel value (false/null respectively). For array_search with strict mode (third argument true), $arr[$key] is narrowed to the intersection of the needle type and the value type, since strict comparison guarantees the found value has the same type as the needle. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
ed8c14b to
b8de415
Compare
Contributor
|
@VincentLanglet simplified it a bit more. I think its good to go |
staabm
approved these changes
Apr 30, 2026
a2621b7 to
3c1137b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
similar to #5537
Fixes phpstan/phpstan#14537